<template>
  <view>
    <form @submit="formSubmit" :reportSubmit="true">
      <view class="font">
        <view class="fukuan_header">
          <image class="fukuan_img" :src="storeinfo.store.logo"></image>
          <view class="fukuan_seller">{{ storeinfo.store.name }}</view>
        </view>
        <view class="fukuan_pay">
          <view class="fukuan_money">
            <input
              @input="moneyFun"
              class="fukuan_input"
              placeholder="消费金额"
              placeholderStyle="color:#ccc;"
              type="digit"
            />
            <text class="money_text">询问店员后输入</text>
          </view>
          <view class="fukuan_flex">
            <view class="fukuan_add">+</view>
            <view class="youhui">不参与优惠金额</view>
          </view>
        </view>
        <view class="footer">
          <view class="footer_left" :style="'color:' + color"
            >{{ money }}元</view
          >
          <button
            @tap="xszz"
            class="footer_right"
            :style="'background:' + color"
            style="border-radius: 100rpx"
          >
            立即付款
          </button>
        </view>
      </view>
      <view @tap="yczz" class="drawer_screen" v-if="showModal"></view>
      <view class="drawer_box" v-if="showModal">
        <view class="drawer_title">请选择支付方式</view>
        <view class="weui-cells weui-cells_after-title">
          <radio-group
            @change="radioChange"
            class="radio-group"
            name="radiogroup"
          >
            <label>
              <view class="weui-cell">
                <view class="zfimg">
                  <image src="https://yunkeososyunchuc.bc8t.cn/ymq_canyino2o/static/weixin.png"></image>
                </view>
                <view class="weui-cell__bd">微信支付</view>
                <view class="weui-cell__ft">
                  <radio :checked="zffs == 1" value="wxzf"></radio>
                </view>
              </view>
            </label>
            <label v-if="kqyue">
              <view class="weui-cell" style="border-top: 1rpx solid #e6e6e6">
                <view class="zfimg">
                  <image src="https://yunkeososyunchuc.bc8t.cn/ymq_canyino2o/static/yeqb.png"></image>
                </view>
                <view class="weui-cell__bd">
                  余额支付
                  <text class="yetext">您的余额 {{ wallet }}</text>
                </view>
                <view class="weui-cell__ft">
                  <radio
                    :checked="zffs == 2"
                    color="#FF9700"
                    :disabled="!kqyue"
                    value="yezf"
                  ></radio>
                </view>
              </view>
            </label>
          </radio-group>
        </view>
        <view class="drawer_title1">
          您选择的{{ zfwz }}
          <text v-if="zffs == 3">({{ jf_proportion }}积分可抵1元)</text>
        </view>
        <button :class="btntype" :disabled="zfz" formType="submit">
          <view v-if="!zfz">
            确定支付
            <text class="zftext">￥{{ money }}</text>
          </view>
          <view v-if="zfz">
            <text class="zfztext">支付中...</text>
          </view>
        </button>
      </view>
    </form>
  </view>
</template>

<script>
import cardlist from '@/ymq_canyino2o/dist/cardlist/index'
import gettel from '@/ymq_canyino2o/dist/gettel/index'
var form_id
var app = getApp()
export default {
  components: {
    cardlist,
    gettel
  },
  data() {
    return {
      money: 0,
      qzf: true,
      showModal: false,
      zffs: 1,
      zfz: false,
      zfwz: '微信支付',
      btntype: 'btn_ok1',
      userinfo: '',

      storeinfo: {
        store: {
          logo: '',
          name: ''
        }
      },

      kqyue: false,
      url: '',
      ptxx: '',
      jf_proportion: '',
      ptkqyue: false,
      ptkqjf: false,
      form_id: '',
      wallet: '',
      total_score: '',
      color: ''
    }
  },
  onLoad: function (t) {
    this.onLoadClone3389(t)
  },
  onReady: function () {},
  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {
    this.onLoadClone3389({})
    uni.stopPullDownRefresh()
  },
  onReachBottom: function () {},
  onShareAppMessage: function () {},
  methods: {
    onLoadClone3389: function (t) {
      app.globalData.setNavigationBarColor(this)
      var that = this
      console.log(t)
      var e = decodeURIComponent(t.scene)
      if ('undefined' != e) {
        console.log('扫码进入')
        var o = e
      } else {
        o = t.storeid
      }
      console.log('scene', e, o)
      app.globalData.getUserInfo(function (t) {
        console.log(t)
        that.onShow1()
        that.setData({
          userinfo: t
        })
      })
      app.globalData.util.request({
        url: 'entry/wxapp/StoreInfo',
        cachetime: '0',

        data: {
          store_id: o
        },

        success: function (t) {
          console.log(t)
          var e = t.data
          that.setData({
            storeinfo: t.data
          })
          if (
            '1' == getApp().globalData.xtxx.is_yuepay &&
            '1' == e.storeset.is_yuepay
          ) {
            that.setData({
              kqyue: true
            })
          }
        }
      })
      app.globalData.util.request({
        url: 'entry/wxapp/Url',
        cachetime: '0',

        success: function (t) {
          that.setData({
            url: t.data
          })
        }
      })
      app.globalData.util.request({
        url: 'entry/wxapp/system',
        cachetime: '0',

        success: function (t) {
          console.log(t)
          that.setData({
            ptxx: t.data,
            jf_proportion: t.data.jf_proportion
          })
          if ('1' == t.data.is_yue) {
            that.setData({
              ptkqyue: true
            })
          } else {
            that.setData({
              ptkqyue: false
            })
          }
          if ('1' == t.data.is_jfpay) {
            that.setData({
              ptkqjf: true
            })
          } else {
            that.setData({
              ptkqjf: false
            })
          }
        }
      })
    },

    radioChange: function (t) {
      console.log('radio发生change事件，携带value值为：', t.detail.value)
      if ('wxzf' == t.detail.value) {
        this.setData({
          zffs: 1,
          zfwz: '微信支付',
          btntype: 'btn_ok1'
        })
      }
      if ('yezf' == t.detail.value) {
        this.setData({
          zffs: 2,
          zfwz: '余额支付',
          btntype: 'btn_ok2'
        })
      }
      if ('jfzf' == t.detail.value) {
        this.setData({
          zffs: 3,
          zfwz: '积分支付',
          btntype: 'btn_ok3'
        })
      }
    },

    xszz: function () {
      var t = this.userinfo
      console.log(t)
      if ('' == t.img || '' == t.name) {
        uni.navigateTo({
          url: '/ymq_canyino2o/pages/my/userinfo' // ../smdc/getdl
        })
      } else {
        this.setData({
          showModal: true
        })
      }
    },

    yczz: function () {
      this.setData({
        showModal: false
      })
    },

    moneyFun: function (t) {
      var e
      console.log(t.detail.value)
      if ('' != t.detail.value) {
        e = t.detail.value
      } else {
        e = 0
      }
      this.setData({
        money: parseFloat(e).toFixed(2)
      })
    },

    formSubmit: function (e) {
      var that = this
      form_id = e.detail.formId
      that.setData({
        form_id: form_id
      })
      var o = this.userinfo.openid
      var t = this.userinfo.id
      var s = this.money
      var i = this.storeinfo.store.id
      console.log("付款参数","用户openid:"+o, "用户id:"+t, "金额:"+s, "店铺id"+i)
      if (0 == s) {
        uni.showModal({
          title: '提示',
          content: '付款金额不能等于0'
        })
        return false
      }
      console.log(
        'form发生了submit事件，携带数据为：',
        e.detail.value.radiogroup
      )
      if ('yezf' == e.detail.value.radiogroup) {
        var n = Number(this.wallet)
        s = Number(this.money)
        console.log(n, s)
        if (n < s) {
          return void uni.showToast({
            title: '余额不足支付',
            icon: 'loading'
          })
        }
      }
      var l = 0
      if ('jfzf' == e.detail.value.radiogroup) {
        var r = Number(this.total_score) / Number(this.jf_proportion)
        l = (s = Number(this.money)) * Number(this.jf_proportion)
        console.log(r, s, l)
        if (r < s) {
          return void uni.showToast({
            title: '积分不足支付',
            icon: 'loading'
          })
        }
      }
      if ('yezf' == e.detail.value.radiogroup) {
        var d = 2
      }
      if ('wxzf' == e.detail.value.radiogroup) {
        d = 1
      }
      if ('jfzf' == e.detail.value.radiogroup) {
        d = 3
      }
      console.log('pay_type', d)
      if ('' == form_id) {
        uni.showToast({
          title: '没有获取到formid',
          icon: 'loading',
          duration: 1000
        })
      } else {
        this.setData({
          zfz: true
        })
        app.globalData.util.request({
          url: 'entry/wxapp/DmOrder',
          cachetime: '0',
          data: {
            money: s,
            store_id: i,
            user_id: t,
            pay_type: d
          },
          success: function (t) {
            that.setData({
              zfz: false,
              showModal: false
            })
            console.log("当面支付结果:",t)
            if ('下单失败' != t.data) {
              if ('yezf' == e.detail.value.radiogroup) {
                console.log('余额支付流程')
                uni.showModal({
                  title: '提示',
                  content: '支付成功'
                })
                setTimeout(()=>{
                  uni.redirectTo({
                    url: '/ymq_canyino2o/pages/seller/fukuan?storeid=' + i
                  })
                },1000)
              } else {
                if ('jfzf' == e.detail.value.radiogroup) {
                  console.log('积分支付流程')
                } else {
                  console.log('微信支付流程')
                  app.globalData.util.request({
                    url: 'entry/wxapp/pay',
                    cachetime: '0',
                    data: {
                      openid: o,
                      money: s,
                      order_id: t.data
                    },
                    success: function (t) {
                      console.log(t)
                      uni.requestPayment({
                        timeStamp: t.data.timeStamp,
                        nonceStr: t.data.nonceStr,
                        package: t.data.package,
                        signType: t.data.signType,
                        paySign: t.data.paySign,
                        success: function (t) {
                          console.log(t.data)
                          console.log(t)
                          console.log(form_id)
                        },
                        complete: function (t) {
                          console.log(t)
                          if ('requestPayment:fail cancel' == t.errMsg) {
                            uni.showToast({
                              title: '取消支付',
                              icon: 'loading',
                              duration: 1000
                            })
                          }
                          if ('requestPayment:ok' == t.errMsg) {
                            uni.showModal({
                              title: '提示',
                              content: '支付成功'
                            })
                            setTimeout(()=>{
                              uni.redirectTo({
                                url: '/ymq_canyino2o/pages/seller/fukuan?storeid=' + i
                              })
                            },1000)
                          }
                        }
                      })
                    }
                  })
                }
              }
            }
          }
        })
      }
    },

    onShow1: function () {
      var that = this
      var t = uni.getStorageSync('users').id
      app.globalData.util.request({
        url: 'entry/wxapp/UserInfo',
        cachetime: '0',
        data: {
          user_id: t
        },
        success: function (t) {
          console.log(t)
          that.setData({
            wallet: t.data.wallet,
            total_score: t.data.total_score
          })
        }
      })
    }
  }
}
</script>
<style>
@import './fukuan.css';
</style>
